package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.JoinPrepareGrid;
import com.epfresh.supplier.entity.JoinProductItems;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class JoinSalePrepareGridViewBinder extends ItemViewBinder<JoinProductItems.ProductBean, JoinSalePrepareGridViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

//    public String chargeUnit = "斤";
//
//    public String getChargeUnit() {
//        return chargeUnit;
//    }
//
//    public void setChargeUnit(String chargeUnit) {
//        this.chargeUnit = chargeUnit;
//    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    //更新某个位置的数据
    public void notifyItem(int position) {
        if (position > -1 && position < getAdapter().getItemCount())
            getAdapter().notifyItemChanged(position);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_join_prepare_grid, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JoinProductItems.ProductBean post) {

        int position = getPosition(holder);
        holder.ll_all.setTag(R.id.item_key_position, position);
        holder.ll_all.setTag(R.id.item_key_status, post);
        holder.ll_all.setOnClickListener(this);
        holder.tv_number_1.setText(post.getQuantity() + post.getChargeUnit());
        holder.tv_number_comment.setText(post.getQuantity() + post.getChargeUnit());

        if (post.getOrderNumber() != null && post.getOrderNumber().length() > 6) {
            if (post.getPositionNo() != null && post.getPositionNo().length() > 0)
                holder.tv_number_3.setText(post.getOrderNumber().substring(post.getOrderNumber().length() - 6, post.getOrderNumber().length()) + "(" + post.getLineNo() + "-" + post.getPositionNo() + ")");
            else
                holder.tv_number_3.setText(post.getOrderNumber().substring(post.getOrderNumber().length() - 6, post.getOrderNumber().length()));
        } else {
            if (post.getPositionNo() != null && post.getPositionNo().length() > 0)
                holder.tv_number_3.setText(post.getOrderNumber() + "(" + post.getLineNo() + "-" + post.getPositionNo() + ")");
            else
                holder.tv_number_3.setText(post.getOrderNumber());
        }
        if (post.getBuyerName() == null || "null".equals(post.getBuyerName()) || "".equals(post.getBuyerName())) {
            holder.tv_name.setText("收件人");
        } else {
            holder.tv_name.setText(post.getBuyerName() + "(" + post.getReceiver() + ")");
        }


        if (TextUtils.isEmpty(post.getActualQuantity()) || "0".equals(post.getActualQuantity())) {
            holder.tv_number_1.setTextSize(20);
            holder.tv_number_2.setVisibility(View.GONE);
            holder.ll_content.setEnabled(true);
        } else {
            holder.tv_number_1.setTextSize(14);
            holder.tv_number_2.setText(post.getActualQuantity() + post.getChargeUnit());
            holder.tv_number_2.setVisibility(View.VISIBLE);
            holder.ll_content.setEnabled(false);
            holder.ll_comment.setVisibility(View.GONE);
        }

        if (post.getMerMsg() != null && !"".equals(post.getMerMsg())) {
            //holder.tv_number_1.setTextSize(14);
            holder.tv_comment.setText(post.getMerMsg());
            holder.ll_comment.setVisibility(View.VISIBLE);
        } else {
            //holder.tv_number_1.setTextSize(22);
            holder.ll_comment.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);
            JoinProductItems.ProductBean bean = (JoinProductItems.ProductBean) o;
            if (v.getId() == R.id.ll_all) {
                bean.setMerMsg(null);
                notifyItem(position);
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_number_comment;

        TextView tv_comment;
        TextView tv_number_1;
        TextView tv_number_2;
        TextView tv_number_3;
        View ll_all, ll_content, ll_comment;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_all = itemView.findViewById(R.id.ll_all);
            ll_content = itemView.findViewById(R.id.ll_content);
            ll_comment = itemView.findViewById(R.id.ll_comment);
            tv_comment = itemView.findViewById(R.id.tv_comment);
            tv_number_1 = itemView.findViewById(R.id.tv_number_1);
            tv_number_2 = itemView.findViewById(R.id.tv_number_2);
            tv_number_3 = itemView.findViewById(R.id.tv_number_3);

            tv_name = itemView.findViewById(R.id.tv_name);
            tv_number_comment = itemView.findViewById(R.id.tv_number_comment);

        }
    }
}
